package communication;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URI;
import java.util.HashMap;

import executor.CreateFileInDB;
import executor.DeleteFileInDB;
import executor.ExecutorsHash;
import executor.RenameFileInDB;

/**
 * responsible for get the data from the service - from the dirWatcher
 * and update the DB
 * 
 * @author Vladimir Sarkisyan & Erez Levy
 */
public class ProtocolRunable2 implements Runnable{
	BufferedReader in; PrintWriter out;
	
	 public ProtocolRunable2(BufferedReader in, PrintWriter out){
		  this.in=in; this.out=out;
		 }
	 
	 /**
	  * take requests from the dirWatcher that in the service and treat them
	  * 
	  * take requests from the service, that come from the dirWatcher
	  * (listener to the files directory) that in the service, and call 
	  * to the treatTheRequest, that check these strings and update the DB
	  * 
	  */
	@Override
	public void run() {
		// TODO Auto-generated method stub
		  String request="",response="";
		  try {

		  while(true){
			  
			   //System.out.println("in while");
			   request=in.readLine();
			   
			   //System.out.println(request);
			   if(request!=null){
				   treatTheRequest(request);
			   }
			   /*response=hm.get(request);
			    * 
			   if(response!=null) {
				   out.println(response);
				   System.out.println(response);
			   }
			   */
			   
			  
		   }//
		   } catch (IOException e) {}
	}
	//end run
	
	/**
	 * treat the Request and update the DB
	 * 
	 * checking if the file created, deleted, renamed, create 
	 * a executor and update the DB 
	 * 
	 * @param request - came from the service
	 */
	public void treatTheRequest(String request){
		//System.out.println("in ProtocolRunable2 - treatTheRequest");
		//System.out.println("request:  " + request);
		String []arr;
		arr = request.split(" ");
		//System.out.println(arr[0]);
		
		//create in the Data base
		if (arr[0].equals("created")){
    		//System.out.println("in create");
    		CreateFileInDB createFile = (CreateFileInDB) ExecutorsHash.Instance.getExecutor("CreateFileInDB");
    		//set the name of the file
    		createFile.setName(arr[1]);
    		createFile.execute();
		}
		
		else if(arr[0].equals("deleted")){
			//System.out.println("in delete");
			DeleteFileInDB deleteFile = (DeleteFileInDB) ExecutorsHash.Instance.getExecutor("DeleteFileInDB");
    		//set the name of the file
    		deleteFile.setName(arr[1]);
    		deleteFile.execute();
		}
		
		else if(arr[0].equals("rename")){
    		//System.out.println("in rename");
    		RenameFileInDB renameFile = (RenameFileInDB) ExecutorsHash.Instance.getExecutor("RenameFileInDB");
    		
    		//set the old file name
    		renameFile.setOldFileName(arr[1]);

    		//set the new name of the file
    		//for debug 
    		//System.out.println("NewFileName - "+ arr[2]);
    		//
    		renameFile.setNewFileName(arr[2]);
    		renameFile.execute();
		}
	}
	//end of treatTheRequest

}
